#include "TextureShader.h"
#include "TextureVS.h"
#include "TexturePS.h"

TextureShader::TextureShader()
{
}

TextureShader::~TextureShader()
{
}

void TextureShader::Initialize(ID3D11Device* device)
{
	HRESULT result;

	result = device->CreateVertexShader(vertexShaderByteCode, sizeof(vertexShaderByteCode), nullptr, &m_vertexShader);
	result = device->CreatePixelShader(pixelShaderByteCode, sizeof(pixelShaderByteCode), nullptr, &m_pixelShader);

	D3D11_INPUT_ELEMENT_DESC layoutDescription[3];
	layoutDescription[0].SemanticName = "POSITION";
	layoutDescription[0].SemanticIndex = 0;
	layoutDescription[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	layoutDescription[0].InputSlot = 0;
	layoutDescription[0].AlignedByteOffset = 0;
	layoutDescription[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layoutDescription[0].InstanceDataStepRate = 0;
	layoutDescription[1].SemanticName = "NORMAL";
	layoutDescription[1].SemanticIndex = 0;
	layoutDescription[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	layoutDescription[1].InputSlot = 0;
	layoutDescription[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layoutDescription[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layoutDescription[1].InstanceDataStepRate = 0;
	layoutDescription[2].SemanticName = "TEXCOORD";
	layoutDescription[2].SemanticIndex = 0;
	layoutDescription[2].Format = DXGI_FORMAT_R32G32_FLOAT;
	layoutDescription[2].InputSlot = 0;
	layoutDescription[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layoutDescription[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layoutDescription[2].InstanceDataStepRate = 0;
	result = device->CreateInputLayout(layoutDescription, ARRAYSIZE(layoutDescription), vertexShaderByteCode, sizeof(vertexShaderByteCode), &m_layout);

	D3D11_BUFFER_DESC vertexShaderBufferDesc;
	vertexShaderBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	vertexShaderBufferDesc.ByteWidth = sizeof(VertexShaderBufferType);
	vertexShaderBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	vertexShaderBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	vertexShaderBufferDesc.MiscFlags = 0;
	vertexShaderBufferDesc.StructureByteStride = 0;
	device->CreateBuffer(&vertexShaderBufferDesc, NULL, &m_vertexShaderBuffer);

	D3D11_BUFFER_DESC pixelShaderBufferDesc;
	pixelShaderBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	pixelShaderBufferDesc.ByteWidth = sizeof(PixelShaderBufferType);
	pixelShaderBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	pixelShaderBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	pixelShaderBufferDesc.MiscFlags = 0;
	pixelShaderBufferDesc.StructureByteStride = 0;
	result = device->CreateBuffer(&pixelShaderBufferDesc, NULL, &m_pixelShaderBuffer);
}

void TextureShader::Shutdown()
{
	if (m_vertexShaderBuffer)
	{
		m_vertexShaderBuffer->Release();
		m_vertexShaderBuffer = 0;
	}
	if (m_pixelShaderBuffer)
	{
		m_pixelShaderBuffer->Release();
		m_pixelShaderBuffer = 0;
	}
	if (m_layout)
	{
		m_layout->Release();
		m_layout = 0;
	}
	if (m_vertexShader)
	{
		m_vertexShader->Release();
		m_vertexShader = 0;
	}
	if (m_pixelShader)
	{
		m_pixelShader->Release();
		m_pixelShader = 0;
	}
}